//
//  MarqueeView.swift
//  zhonglb_iOS
//
//  Created by lxm on 2022/6/23.
//

import UIKit

class MarqueeView: UIView {

    //MARK: --------------------属性--------------------
    var dataArray = [(String, String)]()
    
    var timer = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
    
    var disposeBag = DisposeBag()
    // 数据不足，需要再次拉取
    var needData : (()->())?

    //MARK: --------------------UI控件--------------------
    let tableView = UITableView()
    
    var currentIndexPath = IndexPath.init(row: 0, section: 0)
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        tableView.isScrollEnabled = false
        tableView.isUserInteractionEnabled = false
        addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.zm.register(nibName: "MarqueeCell", reuserId: "MarqueeCell")
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        timerGo()
    }
    
    func timerGo() {
        disposeBag = DisposeBag()
        timer.subscribe(onNext:{ [unowned self] resp in
            let r = currentIndexPath.row + 1
            // 未滚动出来的条目
            let remain = dataArray.count - 4 - r
            if remain < 20 {
                // 不足20 就向外界请求
                XMLogger.tracking("record---不足20 就向外界请求")
                if let needData = self.needData {
                    needData()
                }
            }
            if r >= dataArray.count - 4 {
                // 剩余不足5行不滚动
                return
            }
            self.currentIndexPath.row = r
            self.tableView.scrollToRow(at: self.currentIndexPath, at: .top, animated: true)
        }).disposed(by: disposeBag)
    }
    
    func reload() {
        tableView.reloadData()
    }
    
    // 重置
    func reset(array : [(String, String)]) {
        
        self.dataArray.removeAll()
        self.dataArray.append(contentsOf: array)
        self.currentIndexPath.row = 0
        self.tableView.reloadData()
        self.timerGo()
    }
    //MARK: --------------------delegate--------------------

}

extension MarqueeView : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MarqueeCell") as! MarqueeCell
        let data = dataArray[indexPath.row]
        cell.name.text = data.0
        cell.content.text = data.1
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 28
    }
}
